package com.bawei.module_mine.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bawei.module_mine.ui.tab.DynamicFragment
import com.bawei.module_mine.ui.tab.LikeFragment
import com.bawei.module_mine.ui.tab.WorksFragment

class TabAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
    
    // 缓存Fragment实例，避免重复创建
    private val fragments = mutableMapOf<Int, Fragment>()
    
    override fun getItemCount(): Int {
        return 3
    }

    override fun createFragment(position: Int): Fragment {
        // 检查是否已经创建过该位置的Fragment
        return fragments.getOrPut(position) {
            when(position) {
                0 -> WorksFragment()
                1 -> DynamicFragment()
                2 -> LikeFragment()
                else -> throw IllegalArgumentException("Invalid position: $position")
            }
        }
    }
    
    // 清理Fragment缓存
    fun clearFragments() {
        fragments.clear()
    }
}